<section id="main-content">
	<section class="wrapper">
		<!--state overview end-->
		<div class="row">
			<div class="col-lg-12">
				<section class="panel">
					<header class="panel-heading">
						编辑权限
					</header>
					<form>
						<div class="panel-body">
							<div class="form-group customerForm">
								<label class="col-sm-2 control-label">公司*</label>
								<div class="col-sm-10" id="companys" name="companys">
									<div class="row">
										<span ng-repeat="x in comp">
											<div class="col-sm-2" style="line-height: normal;">
												<div class="checkbox">
													<label>
														<input type="checkbox" value="{{x.companyId}}" onclick="getDept(this)"> {{x.company}}
													</label>
												</div>
											</div>
										</span>
									</div>
								</div>
							</div>
							<div class="form-group customerForm" id="departments">
								<label class="col-sm-2 control-label">部门*</label>
								<div class="col-sm-10">
									<span class="doprocheck" style="line-height: normal;">
										<div class="checkbox">
											<label>
												<input type="checkbox" id="checkAll" value="ALL"> 全部
											</label>
										</div>
									</span>
								</div>
							</div>
							<div class="form-group customerForm">
								<label class="col-sm-2 control-label">菜单</label>
								<div class="col-sm-10">
									<select class="form-control" name="level1" id="level1" disabled>
										<option value="XXZX">信息中心</option>
										<option value="YYGL">运营管理</option>
										<option value="CGLC">物资管理</option>
										<option value="XZZX">行政中心</option>
										<option value="XTXX">系统管理</option>
										<option value="WDLC">我的流程</option>
										<option value="QT">其他</option>
									</select>
								</div>
							</div>
							<div class="form-group customerForm">
								<label class="col-sm-2 control-label">功能</label>
								<div class="col-sm-10">
									<select class="form-control" name="authorityCode" id="authorityCode" disabled>
									</select>
								</div>
							</div>
							<!--<div class="form-group customerForm">-->
							<!--<label class="col-sm-2 control-label">类型</label>-->
							<!--<div class="col-sm-10">-->
							<!--<select class="form-control" name="type" id="type">-->
							<!--<option value="0">功能</option>-->
							<!--<option value="1">菜单</option>-->
							<!--</select>-->
							<!--</div>-->
							<!--</div>-->
							<div class="form-group customerForm">
								<label class="col-sm-2 control-label">人员*</label>
								<div class="col-sm-10">
									<input type="text" class="form-control" disabled value="{{userId}}" name="userIds" id="userIds">
								</div>
							</div>
							<div class="form-group customerForm">
								<label class="col-sm-2 control-label">权限*</label>
								<div class="col-sm-10" id="authority">
									<div class="row">
										<div class="col-sm-3" style="line-height: normal;">
											<div class="checkbox">
												<label>
													<input type="checkbox" checked value="C"> 增加
												</label>
											</div>
										</div>
										<div class="col-sm-3" style="line-height: normal;">
											<div class="checkbox">
												<label>
													<input type="checkbox" checked value="D"> 删除
												</label>
											</div>
										</div>
										<div class="col-sm-3" style="line-height: normal;">
											<div class="checkbox">
												<label>
													<input type="checkbox" checked value="U"> 修改
												</label>
											</div>
										</div>
										<div class="col-sm-3" style="line-height: normal;">
											<div class="checkbox">
												<label>
													<input type="checkbox" checked value="R"> 查询
												</label>
											</div>
										</div>
									</div>
								</div>
							</div>
							<div class="form-group customerForm">
								<label class="col-sm-2 control-label">说明</label>
								<div class="col-sm-10">
									<textarea class="form-control" id="comments" name="comments" maxlength="120">{{comments}}</textarea>
								</div>
							</div>

						</div>
					</form>
				</section>
				<section class="panel">
					<div class="panel-body">
						<p class="default-buttons">
							<a href="javascript:void(0);" class="button-back btn btn-info" onclick="window.history.back()">返回</a>
							<a href="javascript:void(0);" class="button-next  btn btn-success submit">确认提交</a>
						</p>
					</div>
				</section>

			</div>
		</div>
	</section>
</section>
<script>
	//全选反选
	$("#checkAll").click(function () {
		if (this.checked) {
			$("#departments").find("input[type='checkbox']").each(function (i, n) {
				if ($(n).val() != "ALL") {
					$(n).attr("checked", true)
				}
			});
		} else {
			$("#departments").find("input[type='checkbox']").each(function (i, n) {
				if ($(n).val() != "ALL") {
					$(n).attr("checked", false)
				}
			});
		}
	});
	function getdeptName(deptCode) {
		var content = "";
		if (deptCode == "HFKJ") {
			content = "浩方科技";
		} else if (deptCode == "HFXX") {
			content = "浩方信息";
		} else if (deptCode == "STSBJ") {
			content = "赛特斯北京";
		} else if (deptCode == "STSGD") {
			content = "赛特斯广东";
		} else if (deptCode == "STSJT") {
			content = "赛特斯集团";
		} else if (deptCode == "STSNJ") {
			content = "赛特斯南京";
		} else if (deptCode == "STSSH") {
			content = "赛特斯上海";
		} else if (deptCode == "SYB-RX") {
			content = "融合运维事业部";
		};
		return content;
	}

	function editrole($scope, CertusFormAjax, $routeParams) {
		CertusFormAjax.ajax("companyListByViewCode", {}, function (response) {
			$scope.comp = JSON.parse(response.data);
		});


		this.getDept = function (checkbox) {
			if (checkbox.checked) {
				var html = '<hr/><div class="col-sm-2" id="' + $(checkbox).val() + '">' + getdeptName($(checkbox).val()) + '：</div><div class="col-sm-10"><div class="lis_check">';
				CertusFormAjax.ajax("deptedList", { "companyCode": $(checkbox).val() }, function (response) {
					var data = JSON.parse(response.data);
					for (var i = 0; i < data.length; i++) {
						html += '<div class="lis_input"><div class="checkbox"><label><input type="checkbox"' +
							' onclick="checkonces(this)" value="' + data[i].departmentCode + '"> ' + data[i].departmentName + '</label></div></div>';
					}
					html += '</div></div>';
					$("#departments").append(html);
					$("#checkAll").attr("checked", false);
				});
			} else {
				$("#" + $(checkbox).val()).next().remove();
				$("#" + $(checkbox).val()).prev().remove();
				$("#" + $(checkbox).val()).remove();

				var flag = true;
				$("#departments").find("input[type='checkbox']").each(function (i, n) {
					if ($(n).val() != "ALL") {
						if (!n.checked) {
							flag = false;
						}
					}
				});
				if (flag) {
					$("#checkAll").attr("checked", true);
				} else {
					$("#checkAll").attr("checked", false);
				}
			}
		};

		this.getDept2 = function (checkbox) {
			var html = '<hr/><div class="col-sm-2" id="' + checkbox + '">' + getdeptName(checkbox) + '：</div><div class="col-sm-10" style="min-height: 34px;"><div class="lis_check">';
			CertusFormAjax.ajax("deptedList", { "companyCode": checkbox }, function (response) {
				var data = JSON.parse(response.data);
				for (var i = 0; i < data.length; i++) {
					html += '<div class="lis_input"><div class="checkbox"><label><input type="checkbox"' +
						' onclick="checkonces(this)" value="' + data[i].departmentCode + '"> ' + data[i].departmentName + '</label></div></div>';
				}
				html += '</div></div>';
				$("#departments").append(html);
			});
		};

		//全选反选
		this.checkonces = function (oncesdept) {
			if (oncesdept.checked) {
				var flag = true;
				$("#departments").find("input[type='checkbox']").each(function (i, n) {
					if ($(n).val() != "ALL") {
						if (!n.checked) {
							flag = false;
						}
					}
				});
				if (flag) {
					$("#checkAll").attr("checked", true);
				} else {
					$("#checkAll").attr("checked", false);
				}
			} else {
				$("#checkAll").attr("checked", false);
			}
		};


		var paramd = $routeParams.param;
		var authorityCode = paramd.split("*")[0];
		var userId = paramd.split("*")[1];

		this.xzdepts = function (departs) {
			for (var i = 0; i < departs.length; i++) {
				if (departs[i] == "ALL") {
					$("#departments").find("input[type='checkbox']").each(function (j, n) {
						$(n).attr("checked", "checked");
					});
				} else {
					$("#departments").find("input[type='checkbox']").each(function (k, n) {
						if ($(n).val() == departs[i]) {
							$(n).attr("checked", true);
						};
					});
				}
			}
		};

		CertusFormAjax.ajax('roleinfos', { "authorityCode": authorityCode, "userId": userId },
			function (response) {
				var data = response.data;
				var viewCompany = "";
				if (data.view_company != undefined) {
					viewCompany = data.view_company.split(",");
					for (var i = 0; i < viewCompany.length; i++) {
						$("#companys").find("input[type='checkbox']").each(function (k, n) {
							if ($(n).val() == viewCompany[i]) {
								$(n).attr("checked", "checked");
								getDept2($(n).val());
							}
						});
					}
				}
				var departs = data.view_department.split(",");
				//延迟200毫秒执行，避免在ajax之前加载
				setTimeout(function () {
					xzdepts(departs)
				}, 200);
				var le1 = authorityCode.split("-")[0];
				var le2 = authorityCode.split("-")[1];
				$("#level1").val(le1);
				document.getElementById("authorityCode").options.add(new Option(data.authorityName, le2));
				$("#type").val(data.type);
				$scope.userId = data.userName + "(" + userId + ")";
				$scope.comments = data.comments;

				for (var i = 0; i < data.crud.length; i++) {
					$("#authority").find("input[type='checkbox']").each(function (k, n) {
						if ($(n).val() == data.crud[i]) {
							$(n).attr("checked", "checked");
						}
					});
				}
			});

		$(".submit").on('click', function () {
			var data = {};
			var viewCompany = "";
			$("#companys").find("input[type='checkbox']:checked").each(function (i, n) {
				if (i == 0) {
					viewCompany += $(n).val();
				} else {
					viewCompany += "," + $(n).val();
				}
			});
			if (viewCompany == "") {
				layer.msg("必须选择一个公司!");
				return;
			}
			data.viewCompany = viewCompany;
			var deptCodes = "";
			$("#departments").find("input[type='checkbox']:checked").each(function (i, n) {
				if ($(n).val() != "ALL") {
					if (i == 0) {
						deptCodes += $(n).val();
					} else {
						deptCodes += "," + $(n).val();
					}
				} else {
					deptCodes = "ALL";
					return false;
				}
			});
			data.deptCodes = deptCodes;

			var authority = "";
			$("#authority").find("input[type='checkbox']:checked").each(function (i, n) {
				authority += $(n).val();
			});
			if (authority == "") {
				layer.msg("必须选择一个权限!");
				return;
			}
			data.authority = authority;
			data.type = $("#type").val();
			data.comments = $("#comments").val();
			data.level1 = $("#level1").val();
			data.authorityCode = $("#authorityCode").val();
			data.userId = userId;

			CertusFormAjax.ajax('editRole', { "param": JSON.stringify(data) }, function () {
				layer.msg("修改成功！");
				window.history.back();
			});
		})
	}
</script>